﻿using HardcoreGame.Common.Helpers;
using HardcoreGame.Content.Biomes;
using HardcoreGame.Content.Items.Weapons.Sets.Acid;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.Systems
{
    public class AcidRainWeaponSystem : ModSystem
    {
        private Dictionary<int, float> transformingItems = [];
        private Dictionary<int, Vector2> itemStartPositions = [];
        private Dictionary<int, float> targetHeights = [];
        private readonly int baseSpearItemType = ItemID.Spear;
        public override void PostUpdateEverything()
        {

            if (!Main.raining || !Main.player[Main.myPlayer].InModBiome<AcidRain>())
            {
                transformingItems.Clear();
                itemStartPositions.Clear();
                targetHeights.Clear();
                return;
            }
            for (int i = 0; i < Main.item.Length; i++)
            {
                Item item = Main.item[i];
                if (MyUtils.IsItemExposedToRain(item))
                {
                    if (item.active && item.type == baseSpearItemType)
                    {
                        if (item.playerIndexTheItemIsReservedFor == Main.myPlayer && !item.favorited)
                        {
                            if (item.position.Y < Main.worldSurface * 16 || item.velocity.Y != 0)
                            {
                                if (!transformingItems.ContainsKey(i))
                                {
                                    transformingItems[i] = 0f;
                                    itemStartPositions[i] = item.position;
                                    targetHeights[i] = item.position.Y - 100f;
                                    string weaponName = Lang.GetItemNameValue(item.type);
                                    Main.NewText($"你的{weaponName}正在被酸雨腐蚀...", Color.LimeGreen);
                                }
                                else
                                {
                                    transformingItems[i] += 1f / 60f;
                                    if (transformingItems[i] >= 10f && transformingItems[i] < 20f)
                                    {
                                        float progress = (transformingItems[i] - 10f) / 10f;
                                        float newY = itemStartPositions[i].Y - 100f * progress;

                                        float moveSpeed = 0.2f;
                                        item.position.Y = item.position.Y + (newY - item.position.Y) * moveSpeed;
                                        item.noGrabDelay = 0;

                                        item.velocity = Vector2.Zero;
                                        if (Main.rand.NextBool(3))
                                        {
                                            Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                0f, 0f, 100, new Color(0, 200, 255), 1.5f);
                                            dust.noGravity = true;
                                            dust.velocity *= 0.5f;
                                        }
                                    }
                                    else if (transformingItems[i] >= 20f)
                                    {
                                        item.position.Y = targetHeights[i];
                                        item.velocity = Vector2.Zero;

                                        if (Main.rand.NextBool(2))
                                        {
                                            Vector2 fromPosition = item.Center + new Vector2(
                                                Main.rand.NextFloat(-50f, 50f),
                                                Main.rand.NextFloat(-50f, 50f));

                                            Vector2 direction = item.Center - fromPosition;
                                            direction.Normalize();

                                            Dust dust = Dust.NewDustDirect(fromPosition, 10, 10, DustID.TerraBlade,
                                                direction.X * 3f, direction.Y * 3f, 100, new Color(0, 200, 255), 2f);
                                            dust.noGravity = true;
                                        }

                                        if (transformingItems[i] >= 30f)
                                        {
                                            Vector2 spawnPos = item.Center;
                                            int newItemIndex = Item.NewItem(item.GetSource_FromThis(),
                                                (int)spawnPos.X, (int)spawnPos.Y,
                                                item.width, item.height,
                                                ModContent.ItemType<Miasma>(),
                                                1, false, 0, false);
                                            if (newItemIndex >= 0 && newItemIndex < Main.item.Length)
                                            {
                                                Item newItem = Main.item[newItemIndex];
                                                newItem.velocity = new Vector2(0, -2f);
                                            }
                                            item.TurnToAir();

                                            for (int d = 0; d < 50; d++)
                                            {
                                                Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                    Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-3f, 3f), 100, new Color(0, 200, 255), 2.5f);
                                                dust.noGravity = true;
                                            }
                                            int upgradedSpearItemType = ModContent.ItemType<Miasma>();
                                            string weaponName = Lang.GetItemNameValue(upgradedSpearItemType);
                                            Main.NewText($"你的长矛已被酸雨改造成了[c/00CCFF: {weaponName}]！", Color.Cyan);
                                            transformingItems.Remove(i);
                                            itemStartPositions.Remove(i);
                                            targetHeights.Remove(i);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                List<int> keysToRemove = [];
                foreach (var key in transformingItems.Keys)
                {
                    if (key >= Main.item.Length || !Main.item[key].active || Main.item[key].type != baseSpearItemType)
                    {
                        keysToRemove.Add(key);
                    }
                }

                foreach (int key in keysToRemove)
                {
                    transformingItems.Remove(key);
                    itemStartPositions.Remove(key);
                    targetHeights.Remove(key);
                }
            }
        }
    }
    public class AcidRainWeaponSystem3 : ModSystem
    {
        private Dictionary<int, float> transformingItems = [];
        private Dictionary<int, Vector2> itemStartPositions = [];
        private Dictionary<int, float> targetHeights = [];

        private int baseSpearItemType = 95;
        public override void PostUpdateEverything()
        {
            if (!Main.raining || !Main.player[Main.myPlayer].InModBiome<AcidRain>())
            {
                transformingItems.Clear();
                itemStartPositions.Clear();
                targetHeights.Clear();
                return;
            }
            for (int i = 0; i < Main.item.Length; i++)
            {
                Item item = Main.item[i];
                if (MyUtils.IsItemExposedToRain(item))
                {
                    if (item.active && item.type == baseSpearItemType)
                    {
                        if (item.playerIndexTheItemIsReservedFor == Main.myPlayer && !item.favorited)
                        {
                            if (item.position.Y < Main.worldSurface * 16 || item.velocity.Y != 0)
                            {
                                if (!transformingItems.ContainsKey(i))
                                {
                                    transformingItems[i] = 0f;
                                    itemStartPositions[i] = item.position;
                                    targetHeights[i] = item.position.Y - 100f;
                                    string weaponName = Lang.GetItemNameValue(item.type);
                                    Main.NewText($"你的{weaponName}正在被酸雨腐蚀...", Color.LimeGreen);
                                }
                                else
                                {
                                    transformingItems[i] += 1f / 60f;
                                    if (transformingItems[i] >= 10f && transformingItems[i] < 20f)
                                    {
                                        float progress = (transformingItems[i] - 10f) / 10f;
                                        float newY = itemStartPositions[i].Y - 100f * progress;

                                        float moveSpeed = 0.2f;
                                        item.position.Y = item.position.Y + (newY - item.position.Y) * moveSpeed;
                                        item.noGrabDelay = 0;

                                        item.velocity = Vector2.Zero;
                                        if (Main.rand.NextBool(3))
                                        {
                                            Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                0f, 0f, 100, new Color(0, 200, 255), 1.5f);
                                            dust.noGravity = true;
                                            dust.velocity *= 0.5f;
                                        }
                                    }
                                    else if (transformingItems[i] >= 20f)
                                    {
                                        item.position.Y = targetHeights[i];
                                        item.velocity = Vector2.Zero;

                                        if (Main.rand.NextBool(2))
                                        {
                                            Vector2 fromPosition = item.Center + new Vector2(
                                                Main.rand.NextFloat(-50f, 50f),
                                                Main.rand.NextFloat(-50f, 50f));

                                            Vector2 direction = item.Center - fromPosition;
                                            direction.Normalize();

                                            Dust dust = Dust.NewDustDirect(fromPosition, 10, 10, DustID.TerraBlade,
                                                direction.X * 3f, direction.Y * 3f, 100, new Color(0, 200, 255), 2f);
                                            dust.noGravity = true;
                                        }

                                        if (transformingItems[i] >= 30f)
                                        {
                                            Vector2 spawnPos = item.Center;
                                            int newItemIndex = Item.NewItem(item.GetSource_FromThis(),
                                                (int)spawnPos.X, (int)spawnPos.Y,
                                                item.width, item.height,
                                                ModContent.ItemType<PoisonCorrosion>(),
                                                1, false, 0, false);
                                            if (newItemIndex >= 0 && newItemIndex < Main.item.Length)
                                            {
                                                Item newItem = Main.item[newItemIndex];
                                                newItem.velocity = new Vector2(0, -2f);
                                            }
                                            item.TurnToAir();

                                            for (int d = 0; d < 50; d++)
                                            {
                                                Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                    Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-3f, 3f), 100, new Color(0, 200, 255), 2.5f);
                                                dust.noGravity = true;
                                            }
                                            int upgradedSpearItemType = ModContent.ItemType<PoisonCorrosion>();
                                            string weaponName = Lang.GetItemNameValue(upgradedSpearItemType);
                                            Main.NewText($"你的长矛已被酸雨改造成了[c/00CCFF: {weaponName}]！", Color.Cyan);
                                            transformingItems.Remove(i);
                                            itemStartPositions.Remove(i);
                                            targetHeights.Remove(i);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            List<int> keysToRemove = [];
            foreach (var key in transformingItems.Keys)
            {
                if (key >= Main.item.Length || !Main.item[key].active || Main.item[key].type != baseSpearItemType)
                {
                    keysToRemove.Add(key);
                }
            }

            foreach (int key in keysToRemove)
            {
                transformingItems.Remove(key);
                itemStartPositions.Remove(key);
                targetHeights.Remove(key);
            }
        }
    }
    public class AcidRainWeaponSystem2 : ModSystem
    {
        private Dictionary<int, float> transformingItems = [];
        private Dictionary<int, Vector2> itemStartPositions = [];
        private Dictionary<int, float> targetHeights = [];

        private int baseSpearItemType = 157;
        public override void PostUpdateEverything()
        {
            if (!Main.raining || !Main.player[Main.myPlayer].InModBiome<AcidRain>())
            {
                transformingItems.Clear();
                itemStartPositions.Clear();
                targetHeights.Clear();
                return;
            }
            for (int i = 0; i < Main.item.Length; i++)
            {
                Item item = Main.item[i];
                if (MyUtils.IsItemExposedToRain(item))
                {
                    if (item.active && item.type == baseSpearItemType)
                    {
                        if (item.playerIndexTheItemIsReservedFor == Main.myPlayer && !item.favorited)
                        {
                            if (item.position.Y < Main.worldSurface * 16 || item.velocity.Y != 0)
                            {
                                if (!transformingItems.ContainsKey(i))
                                {
                                    transformingItems[i] = 0f;
                                    itemStartPositions[i] = item.position;
                                    targetHeights[i] = item.position.Y - 100f;
                                    string weaponName = Lang.GetItemNameValue(item.type);
                                    Main.NewText($"你的{weaponName}正在被酸雨腐蚀...", Color.LimeGreen);
                                }
                                else
                                {
                                    transformingItems[i] += 1f / 60f;
                                    if (transformingItems[i] >= 10f && transformingItems[i] < 20f)
                                    {
                                        float progress = (transformingItems[i] - 10f) / 10f;
                                        float newY = itemStartPositions[i].Y - 100f * progress;

                                        float moveSpeed = 0.2f;
                                        item.position.Y = item.position.Y + (newY - item.position.Y) * moveSpeed;
                                        item.noGrabDelay = 0;

                                        item.velocity = Vector2.Zero;
                                        if (Main.rand.NextBool(3))
                                        {
                                            Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                0f, 0f, 100, new Color(0, 200, 255), 1.5f);
                                            dust.noGravity = true;
                                            dust.velocity *= 0.5f;
                                        }
                                    }
                                    else if (transformingItems[i] >= 20f)
                                    {
                                        item.position.Y = targetHeights[i];
                                        item.velocity = Vector2.Zero;

                                        if (Main.rand.NextBool(2))
                                        {
                                            Vector2 fromPosition = item.Center + new Vector2(
                                                Main.rand.NextFloat(-50f, 50f),
                                                Main.rand.NextFloat(-50f, 50f));

                                            Vector2 direction = item.Center - fromPosition;
                                            direction.Normalize();

                                            Dust dust = Dust.NewDustDirect(fromPosition, 10, 10, DustID.TerraBlade,
                                                direction.X * 3f, direction.Y * 3f, 100, new Color(0, 200, 255), 2f);
                                            dust.noGravity = true;
                                        }

                                        if (transformingItems[i] >= 30f)
                                        {
                                            Vector2 spawnPos = item.Center;
                                            int newItemIndex = Item.NewItem(item.GetSource_FromThis(),
                                                (int)spawnPos.X, (int)spawnPos.Y,
                                                item.width, item.height,
                                                ModContent.ItemType<ErosionTide>(),
                                                1, false, 0, false);
                                            if (newItemIndex >= 0 && newItemIndex < Main.item.Length)
                                            {
                                                Item newItem = Main.item[newItemIndex];
                                                newItem.velocity = new Vector2(0, -2f);
                                            }
                                            item.TurnToAir();

                                            for (int d = 0; d < 50; d++)
                                            {
                                                Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                    Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-3f, 3f), 100, new Color(0, 200, 255), 2.5f);
                                                dust.noGravity = true;
                                            }
                                            int upgradedSpearItemType = ModContent.ItemType<ErosionTide>();
                                            string weaponName = Lang.GetItemNameValue(upgradedSpearItemType);
                                            Main.NewText($"你的长矛已被酸雨改造成了[c/00CCFF: {weaponName}]！", Color.Cyan);
                                            transformingItems.Remove(i);
                                            itemStartPositions.Remove(i);
                                            targetHeights.Remove(i);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            List<int> keysToRemove = [];
            foreach (var key in transformingItems.Keys)
            {
                if (key >= Main.item.Length || !Main.item[key].active || Main.item[key].type != baseSpearItemType)
                {
                    keysToRemove.Add(key);
                }
            }

            foreach (int key in keysToRemove)
            {
                transformingItems.Remove(key);
                itemStartPositions.Remove(key);
                targetHeights.Remove(key);
            }
        }
    }
    public class AcidRainWeaponSystem1 : ModSystem
    {
        private Dictionary<int, float> transformingItems = [];
        private Dictionary<int, Vector2> itemStartPositions = [];
        private Dictionary<int, float> targetHeights = [];

        private int baseSpearItemType = ItemID.PlatinumBow;
        public override void PostUpdateEverything()
        {
            if (!Main.raining || !Main.player[Main.myPlayer].InModBiome<AcidRain>())
            {
                transformingItems.Clear();
                itemStartPositions.Clear();
                targetHeights.Clear();
                return;
            }
            for (int i = 0; i < Main.item.Length; i++)
            {
                Item item = Main.item[i];
                if (MyUtils.IsItemExposedToRain(item))
                {
                    if (item.active && item.type == baseSpearItemType)
                    {
                        if (item.playerIndexTheItemIsReservedFor == Main.myPlayer && !item.favorited)
                        {
                            if (item.position.Y < Main.worldSurface * 16 || item.velocity.Y != 0)
                            {
                                if (!transformingItems.ContainsKey(i))
                                {
                                    transformingItems[i] = 0f;
                                    itemStartPositions[i] = item.position;
                                    targetHeights[i] = item.position.Y - 100f;
                                    string weaponName = Lang.GetItemNameValue(item.type);
                                    Main.NewText($"你的{weaponName}正在被酸雨腐蚀...", Color.LimeGreen);
                                }
                                else
                                {
                                    transformingItems[i] += 1f / 60f;
                                    if (transformingItems[i] >= 10f && transformingItems[i] < 20f)
                                    {
                                        float progress = (transformingItems[i] - 10f) / 10f;
                                        float newY = itemStartPositions[i].Y - 100f * progress;

                                        float moveSpeed = 0.2f;
                                        item.position.Y = item.position.Y + (newY - item.position.Y) * moveSpeed;
                                        item.noGrabDelay = 0;

                                        item.velocity = Vector2.Zero;
                                        if (Main.rand.NextBool(3))
                                        {
                                            Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                0f, 0f, 100, new Color(0, 200, 255), 1.5f);
                                            dust.noGravity = true;
                                            dust.velocity *= 0.5f;
                                        }
                                    }
                                    else if (transformingItems[i] >= 20f)
                                    {
                                        item.position.Y = targetHeights[i];
                                        item.velocity = Vector2.Zero;

                                        if (Main.rand.NextBool(2))
                                        {
                                            Vector2 fromPosition = item.Center + new Vector2(
                                                Main.rand.NextFloat(-50f, 50f),
                                                Main.rand.NextFloat(-50f, 50f));

                                            Vector2 direction = item.Center - fromPosition;
                                            direction.Normalize();

                                            Dust dust = Dust.NewDustDirect(fromPosition, 10, 10, DustID.TerraBlade,
                                                direction.X * 3f, direction.Y * 3f, 100, new Color(0, 200, 255), 2f);
                                            dust.noGravity = true;
                                        }

                                        if (transformingItems[i] >= 30f)
                                        {
                                            Vector2 spawnPos = item.Center;
                                            int newItemIndex = Item.NewItem(item.GetSource_FromThis(),
                                                (int)spawnPos.X, (int)spawnPos.Y,
                                                item.width, item.height,
                                                ModContent.ItemType<AcidRainBow>(),
                                                1, false, 0, false);
                                            if (newItemIndex >= 0 && newItemIndex < Main.item.Length)
                                            {
                                                Item newItem = Main.item[newItemIndex];
                                                newItem.velocity = new Vector2(0, -2f);
                                            }
                                            item.TurnToAir();

                                            for (int d = 0; d < 50; d++)
                                            {
                                                Dust dust = Dust.NewDustDirect(item.position, item.width, item.height, DustID.TerraBlade,
                                                    Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-3f, 3f), 100, new Color(0, 200, 255), 2.5f);
                                                dust.noGravity = true;
                                            }
                                            int upgradedSpearItemType = ModContent.ItemType<AcidRainBow>();
                                            string weaponName = Lang.GetItemNameValue(upgradedSpearItemType);
                                            Main.NewText($"你的长矛已被酸雨改造成了[c/00CCFF: {weaponName}]！", Color.Cyan);
                                            transformingItems.Remove(i);
                                            itemStartPositions.Remove(i);
                                            targetHeights.Remove(i);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                List<int> keysToRemove = [];
                foreach (var key in transformingItems.Keys)
                {
                    if (key >= Main.item.Length || !Main.item[key].active || Main.item[key].type != baseSpearItemType)
                    {
                        keysToRemove.Add(key);
                    }
                }

                foreach (int key in keysToRemove)
                {
                    transformingItems.Remove(key);
                    itemStartPositions.Remove(key);
                    targetHeights.Remove(key);
                }
            }
        }
    }
}